/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var attendee={};
var member={};

$(document).ready(function() {
    init();
    
    $('img.datepicker').each(function (e, img){
        var input = $(img).parent().siblings().children('input');
        Calendar.setup({
            inputField : input.attr('id'),
            trigger    : img.id,
            dateFormat : "%d/%m/%Y",
            onSelect   : function() {
                this.hide()
            }
        });
    });
});

function init(){
    bind_attendee();       
}

function obtainMember(ok_callback, error_callback, callback) {
    var urlBusqueda =  envAppHome + '/pre-registro/member';
    var first = true;   
    
    $('.filtroMember').each(function(index, item) {
        var name = $(item).attr('name');
        var val = $(item).val();                

        if (val != null && val.length > 0) {
            var colon = name.indexOf(':');
            if (colon > 0){
                name = name.substr(colon+1, name.length);
            }
            
            if (first) {
                urlBusqueda += '?';
            } else {
                urlBusqueda += '&';
            }
            urlBusqueda += name + '=' + escape(val);
            first = false;
        }
    });
    var call = $.ajax(
    {
        url:urlBusqueda,
        contentType:'application/json',
        dataType:'json',
        type:'POST',
        success:ok_callback,
        error:error_stub(error_callback),
        complete:callback
    }
    );
}

function loadMember(member){
    $('.primer_nombre').val(member.firstName);
    $('.segundo_nombre').val(member.middleName);
    $('.primer_apellido').val(member.lastName);
    $('.nickName').val(member.firstName);
    
    $('.memberId').val(member.memberId);
    $('.category').val(member.category != null ? member.category.id : null);
    $('.country').val(member.country != null ? member.country.id : null);
    $('.residence').val(member.country != null ? member.country.id : null);
    
    $('.email').val(member.email);    

    bind_attendee();
}

function loadAttendee(attendee){
    $('.primer_nombre').val(attendee.firstName);
    $('.segundo_nombre').val(attendee.middleName);
    $('.primer_apellido').val(attendee.lastName);
    $('.segundo_apellido').val(attendee.secondLastName);
    $('.nickName').val(attendee.nickName);
    
    $('.country').val(attendee.country != null ? attendee.country.id : null);
    $('.memberId').val(attendee.memberId);
    $('.residence').val(attendee.residence != null ? attendee.residence.id : null);
    $('.category').val(attendee.category != null ? attendee.category.id : null);
    
    $('.phone').val(attendee.phone);
    $('.email').val(attendee.email);
    $('.job').val(attendee.job);
    $('.verification').val(attendee.memberVerification);

    bind_attendee();
}

function obtainAttendee(ok_callback, error_callback, callback) {
    var urlBusqueda =  envAppHome + '/pre-registro/participante';
    var first = true;   
    
    $('.filtro').each(function(index, item) {
        var name = $(item).attr('name');
        var val = $(item).val();                

        if (val != null && val.length > 0) {
            var colon = name.indexOf(':');
            if (colon > 0){
                name = name.substr(colon+1, name.length);
            }
            
            if (first) {
                urlBusqueda += '?';
            } else {
                urlBusqueda += '&';
            }
            urlBusqueda += name + '=' + escape(val);
            first = false;
        }
    });
    
    var call = $.ajax(
    {
        url:urlBusqueda,
        contentType:'application/json',
        dataType:'json',
        type:'POST',
        success:ok_callback,
        error:error_stub(error_callback),
        complete:callback
    }
    );
}

function bind_attendee(){
    
    if ($('.residence').val() != 50 && $('.residence').val() != ""){
        $('#transportationInfo').show();
        $('#lodgingHotel').show();
    } else {
        $('#transportationInfo').hide();
        $('#lodgingHotel').hide();
    }
    if ($('.transportation').is(':checked')){
        console.log('transportation true');
        $('#lodgingInfo').show();
    } else {
        $('#lodgingInfo').hide();
    } 
    if ($('.departureTransportation').is(':checked')){
        console.log('departureTransportation true');
        $('#departureLodgingInfo').show();
    } else {
        $('#departureLodgingInfo').hide();
    }     
    
    if ($('.delegationLeader').is(':checked')){
        console.log('delegationLeader true');
        $('#leader').hide();
    } else {
        $('#leader').show();
    }
    if ($('.roomLeader').is(':checked')){
        console.log('delegationLeader true');
        $('#roomHead').hide();
    } else {
        $('#roomHead').show();
    } 
        
    $('.primer_nombre').change(function(){
        copyToNickName($(this).val());
    });
    $('.memberId').change(function(){
        verifyAttendee();
    });
    $('.residence').change(function(){
        if ($(this).val() != 50){
            $('#transportationInfo').show();
            $('#lodgingHotel').show();
        } else {
            $('#transportationInfo').hide();
            $('#lodgingHotel').hide();
        }
    });
    $('.transportation').change(function(){
        var val = $(this).is(':checked');
        console.log('transportation ' + val);
        if (val){
            $('#lodgingInfo').show();
        } else {
            $('#lodgingInfo').hide();
        }
    });    
}

function copyToNickName(firstName){
    if ($('.nickName').val() == null || $('.nickName').val() == ''){
        $('.nickName').val(firstName);
    }
}

function verifyMember(){
    obtainMember(function(d){
        member = d;
        if (member != null && member.id != null){
            loadMember(member);
        }
    })
}

function verifyAttendee(){
    obtainAttendee(function(d){
        attendee = d;
        if (attendee != null && attendee.id != null){
            loadAttendee(attendee);
            if (attendee.firstName == firstName && attendee.lastName == lastName){
                alert('Ya existe este participante');
            }
        } else {
            verifyMember();
        }
    });    
}

function error_stub(error_callback) {
    return function (xhr) {
        if(error_callback) {
            error_callback(xhr);
        } else {
            alertar(xhr.responseText);
        }
    }
}

function alertar(id, mensaje, callback){
    alert('Error! '+id);
}